package com.cain.calendar.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cain.calendar.R;
import com.cain.calendar.dao.CalendarDAO;
import com.cain.calendar.tool.*;

import java.util.Calendar;

/**
 * Created by Cain on 2016/6/25.
 * 日历表格适配器
 */
public class CalendarAdapter extends BaseAdapter {

    private Context context;
    private int currentYear;   //当前年份
    private int currentMonth;   //当前月份
    private int firstDayOfWeek;   //当月第一天是周几
    private Item defChosenItem = null;   //第一天的item

    public CalendarAdapter(Context context, int currentYear, int currentMonth) {
        this.context = context;
        this.currentYear = currentYear;
        this.currentMonth = currentMonth;

        Calendar currentDay = Calendar.getInstance();
        currentDay.set(currentYear, currentMonth, 1);
        firstDayOfWeek = currentDay.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 设置当前年份
     * @param year
     */
    public void setCurrentYear(int year) {
        this.currentYear = year;
    }

    /**
     * 获取当前年份
     * @return
     */
    public int getCurrentYear() {
        return currentYear;
    }

    /**
     * 设置当前月份
     * @param month
     */
    public void setCurrentMonth(int month) {
        this.currentMonth = month;
    }

    /**
     * 获取当前月份
     * @return
     */
    public int getCurrentMonth() {
        return currentMonth;
    }

    /**
     * 获取默认选中的item
     * @return
     */
    public Item getDefChosenItem() {
        return defChosenItem;
    }

    /**
     * 获取前一个月
     * @return
     */
    public int[] getMonthAhead() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(currentYear, currentMonth, 1, 0, 0, 0);
        calendar.add(Calendar.MONTH, -1);
        int[] time = new int[2];
        time[0] = calendar.get(Calendar.YEAR);
        time[1] = calendar.get(Calendar.MONTH);
        return time;
    }

    /**
     * 获取后一个月
     * @return
     */
    public int[] getMonthNext() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(currentYear, currentMonth, 1, 0, 0, 0);
        calendar.add(Calendar.MONTH, 1);
        int[] time = new int[2];
        time[0] = calendar.get(Calendar.YEAR);
        time[1] = calendar.get(Calendar.MONTH);
        return time;
    }

    public class Item {
        public int year;
        public int month;
        public int day;
        public RelativeLayout panel;   //背景面板
        public TextView dateTextView;   //日期
        public TextView lunarTextView;   //农历
        public ImageView eventView;   //是否有事件
    }

    @Override
    public int getCount() {
        return MyCalendar.getNumberOfDays(currentYear, currentMonth) + firstDayOfWeek - 1;
    }

    @Override
    public Object getItem(int position) {
        if (position < firstDayOfWeek - 1)
            return null;
        else {
            Calendar currentDay = Calendar.getInstance();
            currentDay.set(currentYear, currentMonth, position - firstDayOfWeek + 2);
            return currentDay;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Item item;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.calendar_item, null);
            item = new Item();
            item.panel = (RelativeLayout) convertView.findViewById(R.id.calendar_item_panel);
            item.dateTextView = (TextView) convertView.findViewById(R.id.calendar_item_date);
            item.lunarTextView = (TextView) convertView.findViewById(R.id.calendar_item_lunar);
            item.eventView = (ImageView) convertView.findViewById(R.id.calendar_item_event);
            convertView.setTag(item);
        } else {
            item = (Item) convertView.getTag();
        }

        Calendar currentDay = (Calendar) getItem(position);
        if (currentDay != null) {
            item.year = currentDay.get(Calendar.YEAR);
            item.month = currentDay.get(Calendar.MONTH);
            item.day = currentDay.get(Calendar.DAY_OF_MONTH);
            if (MyCalendar.isToday(currentYear, currentMonth, currentDay.get(Calendar.DAY_OF_MONTH))) {
                item.panel.setBackgroundResource(R.drawable.calendar_item_today);
                item.lunarTextView.setTextColor(Color.WHITE);
                defChosenItem = item;
            } else {
                if (position == firstDayOfWeek - 1)
                    defChosenItem = item;
                item.panel.setBackgroundResource(R.drawable.calendar_item_normal);
                item.lunarTextView.setTextColor(Constant.getColor(context, R.color.item_color_lunar));
            }
            item.dateTextView.setText(String.valueOf(currentDay.get(Calendar.DAY_OF_MONTH)));
            LunarCalendar lunarCalendar = new LunarCalendar(currentDay);
            if (lunarCalendar.getDay() == 1)
                item.lunarTextView.setText(lunarCalendar.getMonthStr());
            else
                item.lunarTextView.setText(lunarCalendar.getDayStr());
            if (CalendarDAO.isDayHasEvent(item.year, item.month + 1, item.day))
                item.eventView.setVisibility(View.VISIBLE);
            else
                item.eventView.setVisibility(View.INVISIBLE);
        }
        else {
            item.year = 0;
            item.month = 0;
            item.day = 0;
            item.panel.setBackgroundResource(R.drawable.calendar_item_normal);
            item.dateTextView.setText("");
            item.lunarTextView.setText("");
            item.eventView.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    @Override
    public void notifyDataSetChanged() {
        Calendar currentDay = Calendar.getInstance();
        currentDay.set(currentYear, currentMonth, 1);
        firstDayOfWeek = currentDay.get(Calendar.DAY_OF_WEEK);
        defChosenItem = null;
        super.notifyDataSetChanged();
    }
}
